<?php
namespace App\Models;

use DB;
use Illuminate\Database\Eloquent\Model;

class TopicM extends Model
{
    protected $table='lee_topic';
    static public $STable='lee_topic';

    
    /**
     * 加载 话题基本信息
     */
    static public function  loadTpcBase(){
        return self::all(['fid','title','title_en','content'])->toArray();
    }
    
    
    /**
     * load all topic 
     */
    static public function loadAllTpc($userid){
        $tpcT=self::$STable;
        $tpcFT=TopicFavourM::$STable;
        $tpcFolT=TopicFollowM::$STable;
        
        $sql="select tpc.fid,tpc.title,tpc.title_en,tpc.content,
            tpc.imgpath,tpc.bg_imgpath as bg_tpcicon,tpc.avatar_imgpath as avatar_tpcicon,
            tpc.bebrowsed,tpc.beliked,tpc.iconnum ,tpc.follownum,tpc.width,tpc.height,
            tpcF.userid as favourstate,
            tpcFl.userid as followstate
            from $tpcT  as tpc
            
            LEFT OUTER JOIN $tpcFT as tpcF
            ON tpcF.userid=$userid AND tpc.fid=tpcF.topicid 
            
            LEFT OUTER JOIN $tpcFolT as tpcFl
            ON tpcFl.userid=$userid AND tpc.fid=tpcFl.topicid
            
            order by tpc.id  ";
        return DB::select($sql);
    }
    

    /**
     * add new topic
     * @param array $data
     * array ['title'=>"title1",'title_en'=>'en title','content'=>"content"]
     */
    static public function addTpc(array $data){
        $tpcInst=new TopicM();
        foreach ($data as $key => $value){
            $tpcInst->$key=$value;
        }
        $tpcInst->save();
    }
    
    /**
     * 依据 topicid 返回其详细信息
     * @param unknown $tpcid
     */
    static public function loadTpcInfo($tpcid){
        return  self::where('id',$tpcid)->
            select(['title','title_en','content','imgpath', 'bg_imgpath AS bg_tpcicon',
                'avatar_imgpath AS avatar_tpcicon','bebrowsed','beliked','follownum','iconnum'])->get();
          
    }
    
    /**
     *  浏览数自增
     * @param unknown $tpcID
     */
    static public  function plusTcpBrowse($tpcID){
        self::where('id',$tpcID)->increment('bebrowsed',1);
    }
    
    /**
     *  浏览数 自减
     * @param unknown $tpcID
     */
    static public  function minTcpBrowse($tpcID){
        self::where('id',$tpcID)->decrement('bebrowsed',1);
    }
    
    
    
    /**
     *   icon数 自增
     * @param unknown $tpcID
     */
    static public  function plusTcpIcnNum($tpcID){
        self::where('id',$tpcID)->increment('iconnum',1);
    }
    
    /**
     *  icon数 自减
     * @param unknown $tpcID
     */
    static public  function minTcpIcnNum($tpcID){
        self::where('id',$tpcID)->decrement('iconnum',1);
    }
    
    
    /**
     *  topic的icon数 批量++
     * @param array $icnIDS
     */
    static  public function plusTcpIcnS(array $icnIDS){
        $t=self::$STable;
        $iconids=implode(',', $icnIDS);
        $sql="update $t set iconnum=iconnum+1 where id in ( $iconids )";
        DB::select($sql);
    }
    
    
    /**
     *  topic的icon数 批量--
     * @param array $icnIDS
     */
    static  public function minTcpIcnS(array $icnIDS){
        $t=self::$STable;
        $iconids=implode(',', $icnIDS);
        $sql="update $t set iconnum=iconnum-1 where id in ( $iconids )";
        DB::select($sql);
    }
    

    /**
     *   收藏数 自增
     * @param unknown $tpcID
     */
    static public  function plusTcpFavour($tpcID){
        self::where('id',$tpcID)->increment('beliked',1);
    }
    
    /**
     *  收藏数 自减
     * @param unknown $tpcID
     */
    static public  function minTcpFavour($tpcID){
        self::where('id',$tpcID)->decrement('beliked',1);
    }
    
    
    /**
     *   粉丝数 自增
     * @param unknown $tpcID
     */
    static public  function plusTcpFollow($tpcID){
        self::where('id',$tpcID)->increment('follownum',1);
    }
    
    /**
     *  粉丝数 自减
     * @param unknown $tpcID
     */
    static public  function minTcpFollow($tpcID){
        self::where('id',$tpcID)->decrement('follownum',1);
    }
    
    
}

?>